package dse;

import java.io.Serializable;

import dse.messages.singleversion.*;

public class DataRequest implements Serializable, Comparable<DataRequest> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int READ = 0;
	public static final int WRITE = 1;

	private Version timestamp;
	private int type;
	private int lockNumber;
	private Node leader;
	private LockProcessor processor;
	private int tranxId;
	private int dataId;
	private int clientID;
	private boolean granted;
	private boolean followerReady;
	private int releaseNumber;
	
	public DataRequest(int type, int clientID, int lockNum, Node leader, int trxid, int did){
		this.type = type;
		this.clientID = clientID;
		this.lockNumber = lockNum;
		this.leader = leader;
		this.processor = null;
		this.tranxId = trxid;
		this.dataId = did;
		this.granted = false;
		this.followerReady = false;
		this.releaseNumber = 0;
	}
	
	public Version getTM(){return timestamp;}
	public void setTM(Version tm){this.timestamp=tm;}
	public int getType(){return type;}
	public int getLockNumber(){return lockNumber;}
	public void setLockNumber(int n){lockNumber=n;}
	public Node getLeader(){return this.leader;}
	public void setLeader(Node n){leader = n;}
	public LockProcessor getProcessor(){return processor;}
	public void setProcessor(LockProcessor p){processor=p;}
	public int getTranxID(){return this.tranxId;}
	public int getDataID(){return this.dataId;}
	public int getClientID(){return this.clientID;}
	public boolean isGranted(){return this.granted;}
	public void setGranted(boolean b){this.granted = b;}
	public boolean isFollowerReady(){return this.followerReady;}
	public void setFollowerReady(boolean b){this.followerReady = b;}
	public int getReleaseNumber(){return this.releaseNumber;}
	public void setReleaseNumber(int n){this.releaseNumber = n;}
	
	@Override
	public int compareTo(DataRequest o) {
		// TODO Auto-generated method stub
		return this.timestamp.compareTo(o.timestamp);
	}
	
	@Override
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("Transaction : " + this.getTranxID() + ", ");
		//sb.append("Requester : " + this.getClientID() + ", ");
		sb.append("TimeStamp : (" + this.getTM().getSequence() + ", " + this.getTM().getClientID() + "), ");
                sb.append("Coordinator: " + this.getLeader().id() + ", ");
		sb.append("Type : ");
		if(this.type == READ)
			sb.append("read");
		else
			sb.append("write");
		return new String(sb);
	}
}
